//import { cwx, _ } from '../cwx/cwx.js';

var CONFIG4COMMON = {
  domain: "https://suzhougs.pang118.com",
  newdomain: "http://cgi.pang118.com",
  linedomain: "https://szgangsong.pang118.com",
};

var api = {

};

/**
 * 完善请求参数
 * @param srcParams
 * @private
 */
function sendPostRequest(url, mParams, options) {
  PostRequest(url, mParams, options);
}

/**
 * 发送 POST 请求（SOA）
 * {"respCode":"200","respMsg":"xxxx","data":[]}
 * @param url
 * @param mParams
 * @param options
 */
function PostRequest(url, mParams, headerParams, options) {
  headerParams["Content-Type"] = "application/x-www-form-urlencoded";
  headerParams["token"] = wx.getStorageSync("AUTH");
  wx.request({
    url: url, //encodeURI(url)
    method: "POST",
    data: mParams,
    header: headerParams,
    success: function(res) {

      var mData = (res || {})['data'] || {};

      if (!mData['respCode']) {
        console.log("接口数据异常");
        options.fail && options.fail(mData['description'] || '接口数据异常');
        return;
      }

      if (mData['respCode'] != "200") {
        console.log("错误错误");
        options.fail && options.fail(mData['description'] || '请求远程服务失败');
        return;
      }
      options.success && options.success(mData['data']);
    },
    fail: function(res) {
      options.fail && options.fail(res['respMsg'] || '请求远程服务失败');
    },
    complete: function(res) {
      options.complete && options.complete(res)
    }
  });
}

/**
 * 发送 POST 请求（SOA）
 * {"code":200,"message":"xxxx","result":[],"status":"OK"}
 * @param url
 * @param mParams
 * @param options
 */
function PostRequest2(url, mParams, headerParams, options) {
  headerParams["Content-Type"] = "application/x-www-form-urlencoded";
  headerParams["token"] = wx.getStorageSync("AUTH");
  wx.request({
    url: url, //encodeURI(url)
    method: "POST",
    data: mParams,
    header: headerParams,
    success: function(res) {

      var mData = (res || {})['data'] || {};

      if (!mData['code']) {
        console.log("接口数据异常");
        options.fail && options.fail(mData['message'] || '接口数据异常');
        return;
      }

      if (mData['code'] != "200") {
        console.log("错误错误");
        options.fail && options.fail(mData['message'] || '请求远程服务失败');
        return;
      }

      options.success && options.success(mData['result']);
    },
    fail: function(res) {
      options.fail && options.fail(res['message'] || '请求远程服务失败');
    },
    complete: function(res) {
      options.complete && options.complete(res)
    }
  });
}

/**
 * 1, 获取商品信息
 * @param mParams
 * @param options
 */
api.getProductList = function requeProductList(mParams, options) {
  var murl = CONFIG4COMMON.linedomain + "/api/device/queryDeviceSaleProducts";
  var headerParams = {};
  PostRequest(murl, mParams, headerParams, options);
}

/**
 * 2, 领取记录
 * @param mParams
 * @param options
 */
api.getRecordList = function requeRecordList(type, mParams, options) {
  var murl;
  //1领取记录 2购买记录
  if (type == '1') {
    murl = CONFIG4COMMON.linedomain + "/api/member/getRecordList";
  } else {
    murl = CONFIG4COMMON.linedomain + "/api/member/getPurchaseOrder";
  }
  var headerParams = {};
  PostRequest2(murl, mParams, headerParams, options);
}

/**
 * 3, 授权手机号
 * @param mParams
 * @param options
 */
api.authorizePhone = function requesAuthorizePhone(mParams, options) {
  var murl = CONFIG4COMMON.linedomain + "/api/customer/authorizePhone";
  var headerParams = {
    'content-type': "application/x-www-form-urlencoded; charset=UTF-8;"
  };
  PostRequest2(murl, mParams, headerParams, options);
}

/**
 * 4, 会员免费领取接口
 * @param mParams
 * @param options
 */
api.getFree = function requeFree(mParams, options) {
  var murl = CONFIG4COMMON.linedomain + "/api/member/freeShip";
  var headerParams = {};
  PostRequest2(murl, mParams, headerParams, options);
}

/**
 * 5, 下单购买
 * @param mParams
 * @param options
 */
api.getPay = function requetPay(mParams, headerParams, options) {
  var murl = CONFIG4COMMON.linedomain + "/api/order/generateOrderWithDiscount";
  //var headerParams = {};
  PostRequest(murl, mParams, headerParams, options);
}

module.exports = api;